package com.aa.http;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;

import com.aa.Common;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class HttpEngine {
	private String mUrl = "http://mimi.mimicall.cn:8888/yaloe/bangdinghaoma.do";
	protected static final String ACTIVITY_TAG = "HttpEngine";
	  public static final String DOWNLOADFILE = "DOWNLOADFILE";
	  private static String ERROR = "<?xml";
	  public static final String GET = "GET";
	  public static final String HTTP = "http://";
	  public static final int MAX_TIME_OUT_COUNT = 3;
	  public static final int MAX_TIME_OUT_TIME = 5000;
	  public static final String POST = "POST";
	  public static final String UPLOADFILE = "UPLOADFILE";
	  private HttpURLConnection connect;
	  private Context context;
	  private String postContent;
	  private String requestParams;
	  private String requestType;
	  private String requestURL;
	  private int sendStatus;
	  private int timeOutCount;
	  private boolean timeOutFlag;
	  
	  public HttpEngine(){
		  
	  }
	  public HttpEngine(String pRequestType, String pRequestURL, String pRequestParams, Context paramContext) {
		  
	    init(pRequestType, pRequestURL, pRequestParams, null, paramContext);
	  }
	  public HttpEngine(String pRequestType, String pRequestURL, String pRequestParams, String pPostContent, Context paramContext){
		  
	    init(pRequestType, pRequestURL, pRequestParams, pPostContent, paramContext);
	  }

	  private String getURL(){
	    StringBuilder _Builder = new StringBuilder(String.valueOf(this.requestURL));
	    if ((requestParams == null) || ("".equals(requestParams)))
	    	return null;
	    _Builder.append("?");
	    _Builder.append(requestParams);
	    return _Builder.toString();
	  }
	  private static String getUrlParams(String pString)
			    throws Exception{
		  
			   
		try{
			   String str1 = pString.replaceAll("\\\\", "/");
			   if (str1.indexOf("?") != -1){
			       String str2 = str1.substring(str1.indexOf("?"));
			       return str2;
			    }
			      return null;
			}catch (Exception e){
			      throw new Exception(HttpEngine.class + " getUrlParams(): url is unresolved!" + e.getMessage());
			}
	}

	  private void init(String pRequestType, String pRequestURL, String pRequestParams, String pPostContent, Context paramContext){
	    try{
	      requestType = pRequestType;
	      requestURL = pRequestURL;
	      requestParams = pRequestParams;
	      postContent = pPostContent;
	      timeOutCount = 0;
	      timeOutFlag = true;
	      sendStatus = -5;
	      requestURL = this.requestURL.replaceAll("\\\\", "/");
	      context = paramContext;
	      return;
	    }
	    catch (Exception localException)
	    {
	    }
	  }

	  public int requestPost()
	  {
	    try
	    {
	      if ((Common.iImsi == null) || (Common.iImsi.length() == 0))
	        Common.iImsi = "0123456789";
	      this.connect = (HttpURLConnection)new URL(mUrl).openConnection();
	      this.connect.setRequestMethod("POST");
	      this.connect.setConnectTimeout(5000);
	      this.connect.setRequestProperty("connection", "keep-alive");
	      this.connect.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	      this.connect.setRequestProperty("Content-Length", String.valueOf(postContent.getBytes().length));
	      this.connect.setRequestProperty("msisdn", Common.iMyPhoneNumber);
	      this.connect.setRequestProperty("product", "9003");
	      this.connect.setRequestProperty("mcid", "9003");
	      this.connect.setRequestProperty("platform", Common.iPlatform);
	      this.connect.setRequestProperty("version", "2.1");
	      this.connect.setRequestProperty("imsi", Common.iImsi);
	      this.connect.setRequestProperty("mu", "");
	      this.connect.setRequestProperty("resolution","480*800");
	      this.connect.setDoInput(true);
	      this.connect.setDoOutput(true);
	      this.connect.setUseCaches(false);
	      while (true)
	      {
	        if (!this.timeOutFlag)
	        {
	          this.sendStatus = 0;
	          return this.sendStatus;
	        }
	        this.timeOutCount = (1 + this.timeOutCount);
	        try
	        {
	          OutputStream localOutputStream = this.connect.getOutputStream();
	          localOutputStream.write(this.postContent.getBytes());
	          localOutputStream.flush();
	          localOutputStream.close();
	          Log.i(ACTIVITY_TAG, localOutputStream.toString());
	          this.timeOutFlag = false;
	        }
	        catch (SocketTimeoutException localSocketTimeoutException)
	        {
	          this.sendStatus = -2;
	          int i = this.sendStatus;
	          return i;
	        }
	      }
	    }
	    catch (MalformedURLException localMalformedURLException)
	    {
	      localMalformedURLException.printStackTrace();
	      this.sendStatus = -5;
	      return this.sendStatus;
	    }
	    catch (IOException localIOException)
	    {
	      while (true)
	      {
	        localIOException.printStackTrace();
	        this.sendStatus = -3;
	      }
	    }
	    catch (Exception localException)
	    {
	      while (true)
	      {
	        localException.printStackTrace();
	        this.sendStatus = -5;
	      }
	    }
	  }
}
